package com.utilcase.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utilcase.domain.Orders;
import com.utilcase.enums.OrderStatus;
import com.utilcase.service.OrdersService;
import com.utilcase.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
* @author 44223
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2024-07-04 11:00:39
*/
@Service
@Slf4j
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{

    @Override
    public boolean checkOrderExists(Long orderId) {
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getId, orderId).eq(Orders::getOrderStatus, OrderStatus.COMPLETED);
        return this.getOne(queryWrapper) != null;
    }

    @Override
    public void updateStatus(String orderId, OrderStatus orderStatus) {
        log.info("更新订单信息");
        LambdaUpdateWrapper<Orders> query = new LambdaUpdateWrapper<>();
        query.eq(Orders::getId, Long.valueOf(orderId));
        query.set(Orders::getOrderStatus, orderStatus);
        update(query);
    }

    @Override
    public String createOrder() {
        Orders orders = new Orders();
        orders.setOrderStatus(OrderStatus.PENDING);
        orders.setUserId(1L);
        orders.setTotalAmount(BigDecimal.valueOf(100));
        save(orders);
        return String.valueOf(orders.getId());
    }
}




